﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;


using BASE;

public partial class Admin_tintuc : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //check quyen vao trang

        if (Utils.getIntSession("userid") == 0)
        {
            Response.Redirect("/admin/login.aspx");
        }
        busMenu busM = new busMenu();
        if (!busM.checkQuyenVaoTrang(Utils.getIntSession("userid"), Constants.ADMIN_TINTHEMMOI))
        {
            Response.Redirect("/admin/");
        }

        //set session forcus trang
        Session["trang"] = Constants.ADMINTINTUC;

        CKFinder.FileBrowser a = new CKFinder.FileBrowser();
        a.SetupCKEditor(CKEditorControl1);
        if (!IsPostBack)
        {
            binLoaiTin();
            if (Utils.getIntQuery("id") != 0)
            {
                entTin entT = new entTin();
                busTin busT = new busTin();
                entT = busT.getTinByTinId(Utils.getIntQuery("id"));
                txt_thutu.Text = entT.ThuTu.ToString();
                txt_tieu_de.Text = entT.TieuDe;
                CKEditorControl1.Text = entT.NoiDung;
                ListItem item = new ListItem();
                item = dr_lt.Items.FindByValue(entT.LoaiTinId.ToString());
                if (item != null)
                {
                    item.Selected = true;
                }
            }
        }
    }
    void binLoaiTin()
    {
        busLoaiTin busLT = new busLoaiTin();
        dr_lt.DataSource = busLT.getAll();
        dr_lt.DataTextField = "TenLoaiTin";
        dr_lt.DataValueField = "LoaiTinId";
        dr_lt.DataBind();
    }
    protected void btn_luu_Click(object sender, EventArgs e)
    {
        entTin entT = new entTin();
        busTin busT = new busTin();
        entT.TinId = Utils.getIntQuery("id");
        entT.TieuDe = txt_tieu_de.Text;
        entT.NoiDung = CKEditorControl1.Text;
        entT.NguoiTao = Utils.getIntSession("userid");
        entT.LoaiTinId = int.Parse(dr_lt.SelectedValue);
        entT.ThuTu = int.Parse(txt_thutu.Text);
        busT.createEdit(entT);
        Response.Redirect("/admin/dstin.aspx");
    }
}